"use strict"
const express = require('express'),
    router = express.Router(),
    User = require('../models/User.js'),
    Content = require('../models/Content.js')

let responseData
router.use((req, res, next) => {
    responseData = {
        code: 0,
        message: ''
    }
    next()
})
router.post('/user/register', (req, res, next) => {

    const body = req.body,
        username = body.username,
        password = body.password,
        repassword = body.repassword

    if (username == '' || password == '') {
        responseData.code = 1
        responseData.message = '用户名或者密码不能为空'
        res.json(responseData)
        return;
    }

    if (password != repassword) {
        responseData.code = 2
        responseData.message = '两次输入的密码不一致'
        res.json(responseData)
        return;
    }

    User.findOne({
        username: username
    }).then((userInfo) => {
        if (userInfo) {
            responseData.code = 3
            responseData.message = '用户名已经被注册了'
            res.json(responseData)
            return
        }

        const user = new User({
            username: username,
            password: password
        })

        req.cookies.set('userInfo', JSON.stringify({
            username: username
        }))
        return user.save()
    }).then((newUserInfo) => {
        responseData.message = '注册成功'
        res.json(responseData)
    })

})

router.post('/user/login', (req, res) => {
    const body = req.body,
        username = body.username,
        password = body.password

    if (username == '' || password == '') {
        responseData.code = 1
        responseData.message = '用户名或者密码不能为空'
        res.json(responseData)
        return
    }

    User.findOne({
        username: username,
        password: password
    }).then((userInfo) => {
        if (!userInfo) {
            responseData.code = 2
            responseData.message = '用户名或者密码错误'
            res.json(responseData)
            return
        }

        responseData.message = '登陆成功'
        responseData.userInfo = {
            _id: userInfo._id,
            username: userInfo.username
        }
        req.cookies.set('userInfo', JSON.stringify({
            _id: userInfo._id,
            username: userInfo.username
        }))
        res.json(responseData)

    })
})

router.get('/user/logout', (req, res) => {
    req.cookies.set('userInfo', null)
    responseData.message = '退出成功'
    res.json(responseData)
})

router.post('/comment/post', (req, res, next) => {
    const contentId = req.body.contentid || ''
    const postData = {
        username: req.userInfo.username,
        postTime: new Date(),
        content: req.body.content
    }

    Content.findOne({
        _id: contentId
    }).then(content => {
        
        content.comments.push(postData)
        return content.save()
    }).then(newContent => {
        console.log(newContent);
        responseData.message = '评论成功'
        responseData.data = newContent
        res.json(responseData)
    }).catch(e => console.error(e))
})

module.exports = router